import { _decorator, Button, Component, instantiate, Label, Node, ScrollView } from 'cc';
import { JSB } from 'cc/env';
const { ccclass, property } = _decorator;

@ccclass('SeegTest')
export class SeegTest extends Component {

    @property({
        type: ScrollView
    })
    private btnSV: ScrollView = null;

    @property({
        type: Node
    })
    private btn: Node = null;

    @property({
        type: ScrollView
    })
    private textSV: ScrollView = null;

    @property({
        type: Node
    })
    private txt: Node = null;


    protected start(): void {
        // 尽早初始化SDK
        // 尽量在回调中进行其它操作，防止需要隐私协议
        seeg.initSdk({
            success: () => {
                this._addText('初始化成功');
                this._initList();
                if (JSB) {
                    this._initAd();
                }

            }
        });
        this._setSdkEvent();
    }

    private _setSdkEvent() {
        // 设置SDK内部事件的监听，并处理
        seeg.setSdkEvent({
            appReview: (callback) => {
                callback(5);
            },
            appUpdate: (callback) => {
                callback({
                    title: '更新',
                    content: '更新内容',
                    submitText: '更新'
                });
            },
            sdkInterstitial: (status) => {
                this._addText('插屏广告状态: ' + status);
            }
        });
    }

    private _initList() {
        const btns =
            [
                // 基础API
                "获取登录信息", "获取游戏配置", "设置存档数据", "获取存档数据", "设置用户信息",
                // 变现API
                "展示横幅", "隐藏横幅", "播放插屏", "播放激励",
                // 应用内购买API
                "查询失败订单", "拉起支付", "消耗订单", "恢复购买",
                // 打点API
                "上报数数事件",
                // 省份排行榜
                "获取所在省份", "上报省份排行榜", "获取省份排行榜",
                // 个人排行榜
                "上报个人排行榜", "获取个人排行榜"
            ];
        const funs =
            [
                this._getLoginData, this._getGameConfig, this._setArchiveData, this._getArchiveData, this._setUserInfo,
                this._showBannerAd, this._hideBannerAd, this._showInterstitialAd, this._showRewardedVideoAd,
                this._queryFailOrder, this._pay, this._consumeOrder, this._restoreOrder,
                this._reportEvent,
                this._getProvinceData, this._setProvinceRankData, this._getProvinceRankData,
                this._setPersonRankData, this._getPersonRankData
            ];
        btns.forEach((btn, index) => {
            this._createBtn(btn, funs[index], index);
        });
    }


    private _initAd() {
        // 小游戏建议传真实广告位id, 否则初始化需要在getGameConfig回调之后才生效
        seeg.initBannerAd({});
        seeg.initInterstitialAd({});
        seeg.initRewardedVideoAd({});
    }

    private _getLoginData() {
        seeg.getLoginData({
            success: (data) => {
                this._addText('获取登录数据成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('获取登录数据失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取登录数据完成');
            }
        });
    }

    private _getGameConfig() {
        seeg.getGameConfig({
            success: (data) => {
                this._addText('获取配置成功: ' + JSON.stringify(data));
                if (!JSB) {
                    this._initAd();
                }
            },
            fail: (err) => {
                this._addText('获取配置失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取配置完成');
            }
        });
    }

    private _setArchiveData() {
        seeg.setArchiveData({
            test: 'test'
        });
    }

    private _getArchiveData() {
        seeg.getArchiveData({
            success: (data) => {
                this._addText('获取存档数据成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('获取存档数据失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取存档数据完成');
            }
        });
    }

    private _setUserInfo() {
        seeg.setUserInfo({
            nickname: 'test',
            avatar: 'https://img.yzcdn.cn/vant/ipad.png',
            gender: 1
        });
    }

    private _showBannerAd() {
        seeg.showBannerAd({});
    }

    private _hideBannerAd() {
        seeg.hideBannerAd({});
    }


    private _showInterstitialAd() {
        seeg.showInterstitialAd({
            positionTag: '测试',
            success: () => {
                this._addText('展示插屏广告成功');
            },
            fail: (err) => {
                this._addText('展示插屏广告失败: ' + JSON.stringify(err));
            }
        });
    }

    private _showRewardedVideoAd() {
        seeg.showRewardedVideoAd({
            position: 1,
            scene: '测试',
            positionTag: '测试',
            success: (res) => {
                this._addText('展示激励广告成功: ' + JSON.stringify(res));
            },
            fail: (err) => {
                this._addText('展示激励广告失败: ' + JSON.stringify(err));
            }
        });
    }

    private _queryFailOrder() {
        seeg.queryFailOrder({
            success: (data) => {
                this._addText('查询失败订单成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('查询失败订单失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('查询失败订单完成');
            }
        });
    }

    private _pay() {
        seeg.pay({
            productId: 'KPV1', // iOS(原心账号 卡皮巴拉)测试商品: KPV1
            userId: '',
            success: (data) => {
                this._addText('支付成功: ' + JSON.stringify(data));
                this.orderId = data.orderId;
            },
            fail: (err) => {
                this._addText('支付失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('支付完成');
            }
        });
    }

    // 订单id，用于消耗订单
    private orderId: string = '';

    private _consumeOrder() {
        seeg.consumeOrder({
            orderId: this.orderId,
            success: (data) => {
                this._addText('消耗订单成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('消耗订单失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('消耗订单完成');
            }
        });
    }

    private _restoreOrder() {
        seeg.restoreOrder({
            success: (data) => {
                this._addText('恢复订单成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('恢复订单失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('恢复订单完成');
            }
        });
    }

    private _reportEvent() {
        seeg.reportEvent("testName", {
            testKey: 'testValue'
        });
    }

    private _getProvinceData() {
        seeg.getProvinceData({
            success: (data) => {
                this._addText('获取用户省份成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('获取用户省份失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取用户省份完成');
            }
        });
    }

    private _setProvinceRankData() {
        seeg.setProvinceRankData({
            type: 'ProvinceTest',
            timeType: seeg.TimeType.WEEK,
            num: 1
        });
    }

    private _getProvinceRankData() {
        seeg.getProvinceRankData({
            type: 'ProvinceTest',
            timeType: seeg.TimeType.WEEK,
            success: (data) => {
                this._addText('获取省份排行榜数据成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('获取省份排行榜数据失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取省份排行榜数据完成');
            }
        });
    }

    private _setPersonRankData() {
        seeg.setPersonRankData({
            type: 'PersonTest',
            timeType: seeg.TimeType.WEEK,
            num: 1
        });
    }

    private _getPersonRankData() {
        seeg.getPersonRankData({
            type: 'PersonTest',
            timeType: seeg.TimeType.WEEK,
            success: (data) => {
                this._addText('获取个人排行榜数据成功: ' + JSON.stringify(data));
            },
            fail: (err) => {
                this._addText('获取个人排行榜数据失败: ' + JSON.stringify(err));
            },
            complete: () => {
                this._addText('获取个人排行榜数据完成');
            }
        });
    }

    private _LevelPass() {
        seeg.trackLevelPass({
            level: 10,
            useTime: 1000,
            revive: false
        });
    }

    private _createBtn(btnName: string, click: Function, index: number) {
        const ins = instantiate(this.btn);
        ins.parent = this.btnSV.content;
        ins.active = true;
        const label = ins.getChildByName('Label')!.getComponent(Label)!;
        label.string = btnName;
        ins.on(Button.EventType.CLICK, click, this);
    }

    private _addText(text: string) {
        if (this.textSV.content.children.length > 5) {
            // 删除第一个
            this.textSV.content.children[0].destroy();
        }
        const ins = instantiate(this.txt);
        ins.active = true;
        ins.parent = this.textSV.content;
        const label = ins.getComponent(Label)!;
        label.string = text;
        this.scheduleOnce(() => {
            this.textSV.scrollToBottom(0.2);
        }, 0.2);
    }
}